<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/2
 * Time: 14:25
 * 加盟商登录
 */

namespace app\franchisee\controller;


use think\Db;

class Apilogin extends Base
{

    /**
     * 账号密码登录
     * @return \think\response\Json
     * phone    手机号
     * pwd      密码
     */
    public function passwordLogin(){
        $phone = trim(input('phone'), ' ');
        $pwd = trim(input('pwd'), ' ');
        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($pwd == '') return json(['status' => 0, 'msg' => '请填写密码']);

        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($user)) return json(['status' => 0, 'msg' => '该手机号尚未注册']);
        if($user['user_role']!=3){//角色不是企业
            return json(['status' => 0, 'msg' => '你的角色不属于加盟商,不能登录']);
        }
        $password = md5($pwd);
        if(!empty($user) && $password != $user['user_password']) return json(['status' => 0, 'msg' => '密码错误']);
        if($user['user_status'] == 2 || $user['user_status'] == 3)
            return json(['status' => 0, 'msg' => '账号异常，请联系管理员']);
        if($user['user_status'] == 5){
            return json(['status' => 101, 'msg' => $user]);
        }
        $token  = md5(date('YmdHis') . rand(100000, 999999));
        $data['user_last_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($data);
        if($res){
            $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
            return json(['status' => 200, 'msg' => $userInfo]);
        }else{
            return json(['status' => 0, 'msg' => '登录失败']);
        }
    }

    /**
     * 发送验证码
     * @return \think\response\Json|void
     * phone       填写的手机号
     * type        发送场景   1 账号密码注册   2 重置密码
     * types       传1  图形验证
     * yzm         图形验证码
     *
     */
    public function getPhoneCode()
    {
        $params = input('');

        if(trim($params['phone'], ' ') == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }

        $userInfo = Db::table('up_u_user')->where('user_phone', $params['phone'])->find();
        if(!empty($userInfo) && isset($params['type']) && $params['type'] == 1) { //注册
            return json(['status' => 0, 'msg' => '该手机号已注册']);
        }
        if(empty($userInfo) && isset($params['type']) && $params['type'] == 2) { //找回密码
            return json(['status' => 0, 'msg' => '该账号不存在']);
        }
        // 图形验证码
        if(isset($params['types']) && $params['types'] == 1){
            $yzm  = input('yzm');
            $yzm_data = Db::table('up_phonecode')->where('phone',$params['phone'])->find();
            if($yzm_data['yzm'] != $yzm){
                return json(['status'=>22,'msg'=>'图形验证码错误']);
            }
            if($yzm_data['yzm_status'] != 1){
                return json(['status'=>22,'msg'=>'此图形验证码已使用，请重新获取']);
            }
            Db::table('up_phonecode')->where('phone',$params['phone'])->update(['yzm_status'=>2]);
        }
//
//        if(!empty($userInfo) && $params['type'] == 4){
//            return json(['status' => 0, 'msg' => '该手机号已注册']);
//        }

        $code = GetRandCode();//6位数验证码
//        $code = '123456';
        $sendres = YzxSms($code, $params['phone'], '',562124);//发送验证码
        if($sendres['code'] != 0) {//发送不成功
            return json(['status' => 0, 'msg' => $sendres['msg']]);
        } else {
            $data = [
                'phone' => $params['phone'],//发送的手机号
                'code' => $code,//验证码
                'time' => time(),//验证码发送的时间戳
                'date_time' => date('Y-m-d H:i:s'),//日期格式
//                'yzm_type' => $params['send_type']
            ];
            $res = Db::table('up_phonecode')->insert($data);
            if(!$res) {
                return json(['status' => 0, 'msg' => '数据库操作失败']);
            }
            return json(['status' => 200, 'msg' => $code]);
        }
    }
    /**
     * 图形验证码
     * phone   手机号
     */
    public function pictureyzm(){
        $phone = input('phone');
        header("content-type:image/png");
        $validateLength=4;
        $strToDraw="";
        $chars=[
            "0","1","2","3","4",
            "5","6","7","8","9",
            "a","b","c","d","e","f","g",
            "h","i","j","k","l","m","n",
            "o","p","q","r","s","t",
            "u","v","w","x","y","z",
            "A","B","C","D","E","F","G",
            "H","I","J","K","L","M","N",
            "O","P","Q","R","S","T",
            "U","V","W","X","Y","Z"
        ];
        $imgW=80;
        $imgH=25;
        $imgRes=imagecreate($imgW,$imgH);
        $imgColor=imagecolorallocate($imgRes,255,255,100);
        $color=imagecolorallocate($imgRes,0,0,0);
        for($i=0;$i<$validateLength;$i++){
            $rand=rand(0,9);
            $strToDraw=$strToDraw.$chars[$rand];
        }
        imagestring($imgRes,5,20,5,$strToDraw,$color);
        for($i=0;$i<100;$i++){
            imagesetpixel($imgRes,rand(0,$imgW),rand(0,$imgH),$color);
        }
        $where['phone'] = $phone;
        $res= Db::table('up_phonecode')->where($where)->find();
        if($res['id']!=''){ // 不等于空的时候
            $data['yzm'] = $strToDraw;
            $data['yzm_status'] = 1;

            Db::table('up_phonecode')->where($where)->update($data);
        }else{//等于空空
            $data['yzm'] = $strToDraw;
            $data['phone'] = $phone;
            Db::table('up_phonecode')->insert($data);
        }
        imagepng($imgRes);
        imagedestroy($imgRes);exit;
    }
    /**
     * 手机号验证码登录/注册
     * @return \think\response\Json
     * phone    手机号
     * code     验证码
     */
    public function phoneLogin(){
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($code == '') return json(['status' => 0, 'msg' => '请填写验证码']);

        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($user)){
            if($user['user_status'] == 2 || $user['user_status'] == 3){
                return json(['status' => 0, 'msg' => '账号异常,请联系管理员']);
            }
        }
        if(!empty($user)){
//            if($user['user_status']==5){
//                return json(['status' => 0, 'msg' => '账号未认证成功,请先认证成功']);
//            }
            if($user['user_role']!=3){//角色不是企业
                return json(['status' => 0, 'msg' => '你的角色不属于加盟商,不能登录']);
            }
        }

        //=========验证验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码超时']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码失效']);

        $token = $token = md5(date('YmdHis') . rand(100000, 999999));
        $data['user_last_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        if($user){ // 登录
            $result = Db::table('up_u_user')->where('user_phone', $phone)->update($data);
            if($result){
                $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
                //修改验证码状态
                Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
                return json(['status' => 200, 'msg' => $userInfo]);
            }else{
                return json(['status' => 0, 'msg' => '快捷登录失败']);
            }
        } else{
            $data['user_phone'] = $phone;
            $data['user_time'] = date('Y-m-d H:i:s');
            $data['user_create_id'] = date('Hi').rand(100,999);
            $userid = Db::table('up_u_user')->insertGetId($data);
            if($userid){
                // 默认用户名    用户+ID
                Db::table('up_u_user')->where('user_id', $userid)->setField('user_username', '用户' . $userid);
                $userInfo = Db::table('up_u_user')->where('user_id', $userid)->find();
                //修改验证码状态
                Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
                // todo:融云
                return json(['status' => 200, 'msg' => $userInfo]);
            }else{
                return json(['status' => 0, 'msg' => '快捷登录失败']);
            }
        }
    }


    /**
     * 重置密码
     * @return \think\response\Json
     * phone     手机号
     * pwd       密码
     * code      验证码
     */
    public function resetPassword1(){
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($code == '') return json(['status' => 0, 'msg' => '请填写验证码']);
        // 账号
        $exist = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($exist)) return json(['status' => 0, 'msg' => '该账号未注册']);
        // 验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码失效']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        $res = Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
        if($res){
            //修改验证码状态
            return json(['status' => 200, 'msg' => '成功']);
        }else{
            return json(['status' => 0, 'msg' => '失败']);
        }
    }
    /**
     * 重置密码
     * @return \think\response\Json
     * phone     手机号
     * pwd       密码
     * code      验证码
     */
    public function resetPassword2(){
        $phone = trim(input('phone'), ' ');
        $pwd = trim(input('pwd'), ' ');
        $oldpwd = trim(input('oldpwd'), ' ');
        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($pwd == '') return json(['status' => 0, 'msg' => '请填写密码']);
        if($oldpwd == '') return json(['status' => 0, 'msg' => '请填写确认密码']);
        if($pwd!=$oldpwd) return json(['status' => 0, 'msg' => '2次密码填写的不一样']);
        // 账号
        $exist = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($exist)) return json(['status' => 0, 'msg' => '该账号未注册']);
        $update = [
            'user_update_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd),
            'user_pass'=>$pwd
        ];
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '重置成功']);
        }else{
            return json(['status' => 0, 'msg' => '重置失败']);
        }
    }
    /**
     * 账号密码注册
     * @return \think\response\Json
     * phone     手机号
     * pwd       密码
     * code      验证码
     */
    public function register(){
        $phone = trim(input('phone'), ' ');
        $pwd = trim(input('pwd'), ' ');
        $code = trim(input('code'), ' ');
        $user_match_lon = trim(input('user_match_lon'), ' ');
        $user_match_lat = trim(input('user_match_lat'), ' ');
        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($code == '') return json(['status' => 0, 'msg' => '请填写验证码']);
        if($pwd == '') return json(['status' => 0, 'msg' => '请填写密码']);

        // 账号
        $exist = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($exist)) return json(['status' => 0, 'msg' => '该账号已注册']);
        // 验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码失效']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        $token = $token = md5(date('YmdHis').rand(100000, 999999));
        $insert = [
            'user_phone' => $phone,
            'user_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd),
            'user_token'=>$token,
            'user_last_time'=>date('Y-m-d H:i:s'),
            'user_create_id'=> date('Hi').rand(100,999),
            'user_role'=> 3,
            'user_match_lon'=>$user_match_lon,
            'user_match_lat'=>$user_match_lat
        ];
        $res = Db::table('up_u_user')->insertGetId($insert);
        if($res){
            // 默认用户名    用户+ID
            Db::table('up_u_user')->where('user_id', $res)->setField('user_username', '用户' . $res);
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            //查询这个用户
            $list = Db::table('up_u_user')->where('user_phone',$phone)->find();
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 0, 'msg' => '注册失败']);
        }
    }








    /**
     * 第三方登录第一步
     * @return \think\response\Json
     * login_type      1 apple   2 微信   3 QQ
     * third_token     微信、QQ传token,apple传ID
     */
    public function thirdLogin(){
        $login_type = input('login_type'); // 1 apple   2 微信   3 QQ
        $third_token = input('third_token'); // 微信、QQ传token，apple传ID
        if($login_type == 1){
            $field = 'user_apple_token';
        }elseif($login_type == 2){
            $field = 'user_wx_token';
        }else{
            $field = 'user_qq_token';
        }
        $first_login = Db::table('up_u_user')->where($field, $third_token)->find();
        if($first_login){ // 有记录
            $token = $token = md5(date('YmdHis') . rand(100000, 999999));
            $update = [
                'user_last_time' => date('Y-m-d H:i:s'),
                'user_token' => $token
            ];
            if($first_login['user_role']!=3){//角色不是企业
                return json(['status' => 0, 'msg' => '你的角色不属于加盟商,不能登录']);
            }
            $res = Db::table('up_u_user')->where('user_id', $first_login['user_id'])->update($update);
            if($res){
                $userInfo = Db::table('up_u_user')->where('user_id', $first_login['user_id'])->find();
                return json(['status' => 200, 'msg' => $userInfo]);
            }else{
                return json(['status' => 0, 'msg' => '登录失败']);
            }
        }else{
            // 第一次登录，先绑定手机
            return json(['status' => 201, 'msg' => '']);
        }
    }

    /**
     * 第三方登录第二步
     * @return \think\response\Json
     * phone           手机号
     * code            验证码
     * login_type      登录方式  1 apple  2 微信  3 QQ
     * third_token     第三方token
     * third_nickname  第三方昵称，apple传空
     * third_image     第三方头像，apple传空
     *
     */
    public function thirdLoginS(){
        $phone = input('phone');
        $code = input('code');
        $login_type = input('login_type');
        $third_token = input('third_token');
        $third_nickname = input('third_nickname');
        $third_image = input('third_image');
        if($login_type == 1){
            $field = 'user_apple_token';
        }elseif($login_type == 2){
            $field = 'user_wx_token';
        }else{
            $field = 'user_qq_token';
        }

        if($phone == '') return json(['status' => 0, 'msg' => '请填写手机号']);
        if($code == '') return json(['status' => 0, 'msg' => '请填写验证码']);
        //=========验证验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码超时']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码失效']);
        $have = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if($have['f_user_id']==''){
//            return json(['status' => 0, 'msg' => '请您先注册,才可以使用第三方登录']);
//        }
        if($have){
            if($have['user_role']!=3){//角色不是企业
                return json(['status' => 0, 'msg' => '你的角色不属于加盟商,不能登录']);
            }
        }
        $have = Db::table('up_u_user')->where('user_phone', $phone)->find();
        $token  = md5(date('YmdHis') . rand(100000, 999999));
        if($have){ // 绑定已存在的手机号
            $update = [
                'user_token' => $token,
                'user_last_time' => date('Y-m-d H:i:s'),
                $field => $third_token,
            ];
            if($login_type == 2){
                $update['user_wx_img'] = $third_image;
                $update['user_wx_nickname'] = $third_nickname;
            }
            if($login_type == 3){
                $update['user_qq_img'] = $third_image;
                $update['user_qq_nickname'] = $third_nickname;
            }
            if(empty($have['user_username'])) $update['user_username'] = $third_nickname;
            if(empty($have['user_image'])) $update['user_image'] = $third_image;
            $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        }else{
            $insert = [
                'user_phone' => $phone,
                'user_token' => $token,
                $field => $third_token,
                'user_time' => date('Y-m-d H:i:s'),
                'user_last_time' => date('Y-m-d H:i:s'),
                'user_create_id'=> md5(date('YmdHis') . rand(100000, 999999))
            ];
            if($login_type == 2){
                $insert['user_wx_img'] = $third_image;
                $insert['user_wx_nickname'] = $third_nickname;
                $insert['user_image'] = $third_image;
                $insert['user_username'] = $third_nickname;
            }
            if($login_type == 3){
                $insert['user_qq_img'] = $third_image;
                $insert['user_qq_nickname'] = $third_nickname;
                $insert['user_image'] = $third_image;
                $insert['user_username'] = $third_nickname;
            }
            $res = Db::table('up_u_user')->insert($insert);
        }
        if($res){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
            // todo:融云
            return json(['status' => 200, 'msg' => $userInfo]);
        }else{
            return json(['status' => 0, 'msg' => '登录异常']);
        }
    }
    /**
     * 用户服务协议
     * @return \think\response\Json
     */
    public function agreement(){
        $agreement = Db::table('up_other')->where('other_id', 1)->value('other_user_agreement');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //第三方设置密码
    public function setPwd(){
        $pwd = trim(input('pwd'), ' ');
        $user_id = trim(input('user_id'),' ');
        $insert = [
            'user_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd)
        ];
        $res = Db::table('up_u_user')->where('user_id', $user_id)->update($insert);
        if($res){
            //修改验证码状态
            return json(['status' => 200, 'msg' =>$res]);
        }else{
            return json(['status' => 0, 'msg' => '重置失败']);
        }
    }
    //认证成功
    public function renzhen1(){
        extract(input());
        //配置您申请的appkey
        $user_real_name = input('param.user_real_name');
        $user_card = input('param.user_card');
        $user_id = input('param.user_id');
        $user_yyzz = input('param.user_yyzz');
        if($user_yyzz!=''){
            $result1 =$this->yyzz($user_yyzz) ;
        }
        $result = $this->card($user_card,$user_real_name);
        if($user_yyzz!=''){
            if($result&&$result1){
                if($result['error_code']=='0'&&$result1['error_code']=='0'){
                    if(strlen($result1['result']['res'][0]['value'])!=18){
                        return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
                    }
                    if($result['result']['res'] == '1'){
                        $data['user_real_name'] = $user_real_name;
                        $data['user_yyzz'] = $user_yyzz;
                        $data['user_card'] = $user_card;
                        $data['user_company_name'] = $result1['result']['res'][8]['value'];
                        $data['user_company_detail'] = $result1['result']['res'][7]['value'];
                        $data['user_jms_address'] = $result1['result']['res'][7]['value'];
                        $data['user_status'] = 1;
                        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
                        $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
                        return json(['status' => 200, 'msg' => $list]);
                    }else{
                        return json(['status' => 0, 'msg' => '身份证号码和真实姓名不一致']);
                    }
                }else{
                    return json(['status' => 0, 'msg' => $result['reason']]);
                }

            }else{
                return json(['status' => 0, 'msg' => '请求失败']);
            }
        }else{
            if($result){
                if($result['error_code']=='0'){
                    if($result['result']['res'] == '1'){
                        $data['user_real_name'] = $user_real_name;
                        $data['user_card'] = $user_card;
                        $data['user_status'] = 1;
                        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
                        $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
                        return json(['status' => 200, 'msg' => $list]);
                    }else{
                        return json(['status' => 0, 'msg' => '身份证号码和真实姓名不一致']);
                    }
                }else{
                    return json(['status' => 0, 'msg' => $result['reason']]);
                }

            }else{
                return json(['status' => 0, 'msg' => '请求失败']);
            }
        }
        //查询这个用户

    }
    //身份证正面反面认证
    public function renzhen2(){
        extract(input());
        $user_id = input('param.user_id');
        $user_z_card_img = input('param.user_z_card_img');
        $user_f_card_img = input('param.user_f_card_img');
        $user_yyzz = input('param.user_yyzz');
        if($user_yyzz!=''){
            $result1 =$this->yyzz($user_yyzz) ;
        }
        $result = $this->card1($user_z_card_img);
        $result2 = $this->card2($user_f_card_img);
        if($user_yyzz!=''){
            if($result['error_code']=='0'&&$result2['error_code']=='0'&&$result1['error_code']=='0'){
                if(strlen($result1['result']['res'][0]['value'])!=18){
                    return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
                }
                if($result['result']['realname']==''){
                    return json(['status' => 0, 'msg' =>"身份证正面上传有误,请上传清晰图片"]);
                }
                if($result2['result']['department']==''){
                    return json(['status' => 0, 'msg' =>"身份证反面上传有误,请上传清晰图片"]);
                }

                $data['user_z_card_img'] = $user_z_card_img;
                $data['user_f_card_img'] = $user_f_card_img;
                $data['user_yyzz'] = $user_yyzz;
                $data['user_real_name'] = $result['result']['realname'];
                $data['user_card'] = $result['result']['idcard'];
                $data['user_company_name'] = $result1['result']['res'][8]['value'];
                $data['user_company_detail'] = $result1['result']['res'][7]['value'];
                $data['user_jms_address'] = $result1['result']['res'][7]['value'];
                $data['user_status'] = 1;
                Db::table('up_u_user')->where('user_id',$user_id)->update($data);
                $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
                return json(['status' => 200, 'msg' => $list]);
            }else{
                if(strlen($result1['result']['res'][0]['value'])!=18){
                    return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
                }
                if($result['result']['realname']==''){
                    return json(['status' => 0, 'msg' =>"身份证正面上传有误,请上传清晰图片"]);
                }
                if($result2['result']['department']==''){
                    return json(['status' => 0, 'msg' =>"身份证反面上传有误,请上传清晰图片"]);
                }
            }
        }else{
           if($result['error_code']=='0'&&$result2['error_code']=='0'){
               if($result['result']['realname']==''){
                   return json(['status' => 0, 'msg' =>"身份证正面上传有误,请上传清晰图片"]);
               }
               if($result2['result']['department']==''){
                   return json(['status' => 0, 'msg' =>"身份证反面上传有误,请上传清晰图片"]);
               }
               $data['user_z_card_img'] = $user_z_card_img;
               $data['user_f_card_img'] = $user_f_card_img;
               $data['user_real_name'] = $result['result']['realname'];
               $data['user_card'] = $result['result']['idcard'];
               $data['user_status'] = 1;
               Db::table('up_u_user')->where('user_id',$user_id)->update($data);
               $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
               return json(['status' => 200, 'msg' => $list]);
           }else{
               if($result['error_code']!='0'){
                   return json(['status' => 0, 'msg' =>"身份证正面上传有误,请上传清晰图片"]);
               }
               if($result2['error_code']!='0'){
                   return json(['status' => 0, 'msg' =>"身份证反面上传有误,请上传清晰图片"]);
               }
           }
        }
    }
    //身份证号码和真实姓名
    public function card($user_card,$user_real_name){
        header('Content-type:text/html;charset=utf-8');
        $url = "http://op.juhe.cn/idcard/query";
        $appkey = "84b9e961866e11a25c3f0e333caa0927";//身份证
        $params = array(
            "idcard" => $user_card,//身份证号码
            "realname" => $user_real_name,//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
        );

        $paramstring = http_build_query($params);
        $content = juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
    }

    //身份证正面
    public function card1($card_z_img){
        header('Content-type:text/html;charset=utf-8');
        $appkey = "7ca337f11f8b827c987126fad505b10d";//身份证
        $url = "http://apis.juhe.cn/idimage/verify";
        $params = array(
            "image" => master_imgUrl_base64($card_z_img),//身份证号码
            "side" => 'front',//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
        );
        $paramstring = http_build_query($params);
        $content = juhecurl($url,$paramstring,1);
        $result = json_decode($content,true);
        return $result;
    }
    //身份证反面
    public function card2($card_f_img){
        header('Content-type:text/html;charset=utf-8');
        $appkey = "7ca337f11f8b827c987126fad505b10d";//身份证
        $url = "http://apis.juhe.cn/idimage/verify";
        $params = array(
            "image" => master_imgUrl_base64($card_f_img),//身份证号码
            "side" => 'back',//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
        );
        $paramstring = http_build_query($params);
        $content = juhecurl($url,$paramstring,1);
        $result = json_decode($content,true);
        return $result;
    }

    //营业执照
    public function yyzz($user_yyzz){
        header('Content-type:text/html;charset=utf-8');
        $appkey1 = "f3cc570bd987000b15338314e118314f";//营业执照
        $user_yyzz1 = master_imgUrl_base64($user_yyzz);//营业执照转base64
        $url1 = "http://apis.juhe.cn/businessLicenseOcr/index";
        $params1 = array(
            "image" => $user_yyzz1,//营业执照
            "key" => $appkey1,//应用APPKEY(应用详细页查询)
        );
        $paramstring1 = http_build_query($params1);
        $content1 = juhecurl($url1,$paramstring1,1);
        $result = json_decode($content1,true);
        return $result;
    }

}